/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package MyPackage;

/**
 *
 * @author Wit
 */
public class Node {

    public Node left;
    public Node right;
    public Node parent;
    public int value;
    public String strValue;
    public int type;    // 0 = value
                        // 1 = operator

    public Node(){
        left = null;
        right = null;
        parent = null;
        value = 0;
        strValue = "None";
        type = 0;
    }

    public void Print(){
        if (parent != null){
            System.out.print("Parent : ");
            if(parent.type==0)
                System.out.print(parent.value);
            else if(parent.type==1)
                System.out.print(parent.strValue);
        }
        else{
            System.out.print("This is root : ");
        }
        System.out.print(" Node : ");
        if(type==0)
            System.out.println(value);
        else if(type==1)
            System.out.println(strValue);

        if (left != null){
            System.out.print("Left : ");
            left.Print();
        }
        if (right != null){
            System.out.print("Right : ");
            right.Print();
        }
    }

    public void PrintInfix(){
        //if(left == null && right == null)
        if(type==0)
            System.out.print(value);
        //if(left != null && right != null)
        else if(type == 1 && left != null && right != null){
            System.out.print("(");
            if(left != null)
                left.PrintInfix();
            System.out.print(strValue);
            if(right != null)
                right.PrintInfix();
            System.out.print(")");
        }
    }

    public void PrintPrefix(){
        //if(left == null && right == null)
        if(type==0){
            System.out.print(value+" ");
        }
        //if(left != null && right != null)
        else if(type == 1 && left != null && right != null){
            System.out.print(strValue+" ");
            if(left != null)
                left.PrintPrefix();
            if(right != null)
                right.PrintPrefix();
        }
    }
    public void PrintPostfix(){
        //if(left == null && right == null)
        if(type==0){
            System.out.print(value+" ");
        }
        //if(left != null && right != null)
        else if(type == 1 && left != null && right != null){
            if(left != null)
                left.PrintPostfix();
            if(right != null)
                right.PrintPostfix();
            System.out.print(strValue+" ");
        }
    }
    public int GetResult(){
        int result=0;
        if(type==0){
            return value;
        }
        //if(left != null && right != null)
        else if(type == 1 && left != null && right != null){
            int leftVal=0;
            int rightVal=0;
            if(left != null)
                leftVal=left.GetResult();
            if(right != null)
                rightVal=right.GetResult();
            if(strValue=="+")
                result=leftVal+rightVal;
            else if(strValue=="-")
                result=leftVal-rightVal;
            else if(strValue=="*")
                result=leftVal*rightVal;
            else if(strValue=="/")
                result=leftVal/rightVal;
        }
        return result;
    }
}
